<?php


/**
** 样式文件
* 2010-8-6
**/

define('IN_ACHAO_ORG', true);
define('ROOT_PATH',     dirname(__FILE__));
define('DEBUG_MODE', 1);

include(ROOT_PATH . '/includes/lib.common.php');
include(ROOT_PATH . '/conf/app.config.php');

define('CHARSET', substr(LANG, strpos(LANG, '-') + 1));
$php_self = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
if ('/' == substr($php_self, -1))
{
    $php_self .= 'mall.php';
}
define('PHP_SELF',  htmlentities($php_self));

$app         = isset($_GET['app']) ? trim($_GET['app']) : '';
$mall_skin   = isset($_GET['mall_skin']) ? trim($_GET['mall_skin']) : '';
$store_skin  = isset($_GET['store_skin']) ? trim($_GET['store_skin']) : '';
$act = isset($_GET['act']) ? trim($_GET['act']) : '';
$style_files = array();

if($app=='mall'){
    $style_files[] = ROOT_PATH . "/themes/mall/skin/default/global.css";
    $style_files[] = ROOT_PATH . "/themes/mall/skin/default/basic.css";
    $style_files[] = ROOT_PATH . "/themes/mall/skin/default/index.css";
}elseif($app=='message' || $app=='error'){
	$style_files[] = ROOT_PATH . "/themes/mall/skin/blue/common.css";
    $style_files[] = ROOT_PATH . "/themes/mall/skin/blue/message.css";
}

//pr($style_files);die();

header("Content-type:text/css\n");

/*fputs(fopen( ROOT_PATH . "/temp/".$app.".log",'a+'),$app.'====='.join(',', $style_files)."\r\n\r\n");

*/
$target = ROOT_PATH . "/temp/compiled/style/" . md5(join('', $style_files)) . '.css';

$modified = false;
$exists   = false;
if (DEBUG_MODE > 0)
{
    $exists = file_exists($target);
    if ($exists)
    {
        $modified = check_modified($style_files, $target);

        if (!$modified && isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
        {
            header('HTTP/1.1 304 Not Modified');
            exit;
        }
    }

    header("Last-Modified:" . date('r'));
}

if ($modified || !$exists )
{
    $code = '';
    foreach($style_files AS $file)
    {
        if (!is_file($file))
        {
            die("hacking");
        }
        else
        {
            $content = file_get_contents($file);
            $skin_img = str_replace(ROOT_PATH , '', dirname($file)) . '/images';
            $skin_img = setDafaultImage("css").substr($skin_img,1);
            $patterns[]     = '/\s+\/\/.*+\n/';
            $replacement[]  = '';
          //  $content = preg_replace($patterns, $replacement, $content);
           // $content = str_replace("\r", '', $content);
           // $content = str_replace("\n", '', $content);
            $content = preg_replace('/url\(\/themes\/images/i', "url($skin_img", $content);
            $content = preg_replace('/url\(themes\/images/i', "url($skin_img", $content);
            $content = preg_replace('/url\(images/i', "url($skin_img", $content);

            $code .= $content;
        }
    }

    file_put_contents($target, $code, LOCK_EX);
}
$file = str_replace(ROOT_PATH . '/', '', $target);
//echo $file;
$string = file_get_contents($file);
echo $string;
//header("Location:$file\n");

/**
 *
 * @author liupeng
 * @param  array  $files
 * @param  array  $target
 * @return  bool
 **/
function check_modified($files, $target)
{
    $mtime = filemtime($target);
    foreach($files AS $val)
    {
        if (filemtime($val) > $mtime)
        {
            return true;
        }
    }
    return false;
}
?>
